package com.bwie.controller;

import bwie.pojo.Goods;
import bwie.unit.R;
import com.bwie.es.GoodsEs;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class GoodsController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @PostMapping("foundEs")
    public R foundEs(){
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(GoodsEs.class);
        boolean exists = indexOperations.exists();
        if (exists){
            indexOperations.delete();
        }
        indexOperations.create();
        Document mapping = indexOperations.createMapping();
        indexOperations.putMapping(mapping);
        return R.OK();
    }

    @PostMapping("pageListEs")
    public R pageListEs(@RequestBody Goods goods, @RequestHeader(name = "token") String token){
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if (!StringUtils.isEmpty(goods.getGoodsName())){
            boolQueryBuilder.must(QueryBuilders.matchQuery("goodsName",goods.getGoodsName()));
        }

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("goodsName").preTags("<font color = 'red'>").postTags("</font>");

        int pageNum = 0;
        if (goods.getPageNum()>0){
            pageNum = goods.getPageNum()-1;
        }
        PageRequest of = PageRequest.of(pageNum, goods.getPageSize());

        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withPageable(of)
                .withHighlightBuilder(highlightBuilder)
                .withQuery(boolQueryBuilder);
        SearchHits<GoodsEs> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), GoodsEs.class);
        long total = search.getTotalHits();
        ArrayList<GoodsEs> list = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        map.put("pageNum",pageNum);
        map.put("pageSize",goods.getPageSize());
        return R.OK(map);

    }

}
